package com.alpha.processor.processor.anno_processor;

import com.alpha.binder.anno.app.BinderApp;
import com.alpha.processor.processor.tools.AnnoProcessorToolException;
import com.alpha.processor.processor.tools.ProcessorTool;
import com.alpha.processor.creators.binder_file.BinderFileCreator;
import com.alpha.processor.processor.anno_processor.base.AnnoProcessor;
import com.alpha.processor.util.ParseTypeUtil;

import java.util.Set;

import javax.annotation.processing.Messager;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;

public class AppAnnoProcessor extends AnnoProcessor {

    private BinderFileCreator binderFileCreator;

    public AppAnnoProcessor(Messager messager, Elements elementUtils, ProcessorTool logger) {
        super(messager, elementUtils, logger);
    }

    @Override
    public void processAnno(RoundEnvironment roundEnvironment) {
        Set<? extends Element> elementSet = roundEnvironment.getElementsAnnotatedWith(BinderApp.class);
        if (elementSet.size() != 1){
            throw new AnnoProcessorToolException("\n\tError: Need One And Only One <@BinderApp> , Add It In <Application> Class;");
        }
        for (Element element : elementSet) {
            BinderApp anno = element.getAnnotation(BinderApp.class);
            logger.updateLogController(anno.printLog());
            TypeElement classElement = (TypeElement) element;
            binderFileCreator = new BinderFileCreator(logger, elementUtils, classElement);
            TypeElement rClassElement = getElementFromType(ParseTypeUtil.fromAppR(anno));
            binderFileCreator.getCreateModel().syncRClassElement(rClassElement);
            break;
        }
    }

    public BinderFileCreator getBinderFileCreator() {
        return binderFileCreator;
    }
}
